package algorithms.que13;

import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdRandom;

/**
 * @功能
 * @创建时间 2020/7/12
 */
public class RandomQueue<Item> {
    private Item[] a = (Item[]) new Object[1];
    private int N = 0;
    public boolean isEmpty(){
        return N==0;
    }
    public int size(){
        return N;
    }
    private void resize(int length){
        Item[] temp = (Item[]) new Object[length];
        for(int i=0;i<N;i++){
            temp[i] = a[i];
        }
        a = temp;
    }
    public void add(Item item){
        if(N == a.length) resize(2*a.length);
        a[N++] = item;
    }
    public Item del(){
        if(N/2== a.length) resize(a.length);
        Item item = a[N];
        a[N--]=null;
        Integer ww = StdRandom.uniform(N);
        Item tmp = (Item) new Object();
        tmp=a[ww];
        a[ww]=a[N];
        a[N]=tmp;
        return item;

    }
}
